package com.ksm.zyz.controller;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.sys.entity.ExtData;
import com.ksm.zyz.sys.entity.mySqltongbu;

/**
 * @author liuhao
 *
 */
@Controller
@RequestMapping("modip")
public class ModIpController extends BaseController<mySqltongbu> {
     private static Logger logger = LoggerFactory.getLogger(IndexController.class);

		@RequestMapping(value = "ip")
	    public String index() {
	        logger.info("进入修改页面了。。。");
	        return "/include/ipmod";
	    }
	
	    @RequestMapping(value ="modip")
	    @ResponseBody
		public ExtData tongbu(HttpServletRequest request,String ip,String date){
	    	System.out.println(ip+"@@@@@@:::::"+date);
			Properties prop = new Properties();
			String path=request.getSession().getServletContext().getRealPath("");
			System.out.println(path);		
	        try{
	           //读取属性文件a.properties
	        	
	            InputStream in = new BufferedInputStream (new FileInputStream(path+"/WEB-INF/classes/a.properties"));
	            prop.load(in);     ///加载属性列表
	            Iterator<String> it=prop.stringPropertyNames().iterator();
	            prop.setProperty("jdbc1.url", "jdbc:sqlserver://"+ip+";DatabaseName="+date);           
	            while(it.hasNext()){
	                String key=it.next();
	                System.out.println(key+":"+prop.getProperty(key));
	            }
	            in.close();
	            
	           ///保存属性到b.properties文件
	            FileOutputStream oFile = new FileOutputStream(path+"/WEB-INF/classes/jdbc.properties", false);//true表示追加打开
	            prop.store(oFile, "The New properties file");
	            oFile.close();
	            System.out.println("修改成功");
	            extData.setTotalCount(200);
	            return extData;
	        }
	        catch(Exception e){
	            System.out.println(e);
	            extData.setTotalCount(404);
	            return extData;
	        }
		}
	   

}
